package com.zzy.template.utils;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.zzy.template.controller.BaseController;

import java.util.Collections;


public class GenCodeUtil {
    public static void genProject(String host,String port,String dbName,String username,String password,String sourcePath,String moduleName,String xmlPath,String ...tableName){
        String prefix = "jdbc:mysql://";
        String suffix = "?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true";
        String url = prefix + host +":"+port+"/"+dbName+suffix;
        FastAutoGenerator.create(url,
                username,
                password)
                .globalConfig(builder -> {
                    builder.author("zzy") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(sourcePath); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.zzy") // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, xmlPath));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableName)
                            .addTablePrefix("t_", "c_")
                            .enableCapitalMode()
                            .entityBuilder()
                            .enableLombok()
                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .enableMapperAnnotation()
                            .controllerBuilder()
                            .superClass(BaseController.class)
                            .enableRestStyle();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
